import axios from 'axios'
import router from '@/router'
import Constants from "./Constants";
import Vue from 'vue';
import ElementUI from 'element-ui';
import md5 from "md5";
export default class AdminApiActions {
    private vm: any;
    constructor(vm: any) {
        this.vm = vm;
    }
    async getSpecGroupPage(params: any) {
        this.vm.isShowTable = false;
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/group/page/list', params);
        this.vm.isShowTable = true;
        return result;
    }
    async getSpecGroup(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/group/list', params);
        return result;
    }
    async addSpecGroup(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/group/add', params);
        return result;
    }
    async updateSpecGroup(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/group/update', params);
        return result;
    }
    //刪除规格参数组
    async deleteSpecGroup(ids: []) {
        let result: any = await axios.post('mshop/api/gataway/item/manager/spec/group/delete', ids);
        return result;
    }
    //获取商品分类信息
    async getCategories(params: any) {
        let result: any = {};
        try {
            result = await axios.post('mshop/api/gataway/item/admin/manager/category/list', params);
        } catch (e) {

        }
        return result;
    }
    //添加商品分类
    async addCategory(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/category/add', params);
        return result;
    }
    //修改商品分类
    async updateCategory(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/category/update', params);
        return result;
    }
    async deleteCategories(ids: []) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/category/delete', ids);
        return result;
    }
    //删除品牌
    async deleteBrand(ids: []) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/brand/delete', ids);
        return result;
    }
    //批量保存参数组接口
    async addSpecGroupBatch(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/group/addBatch', params);
        return result;
    }
    //获取品牌信息
    async getBrandPage(params: any) {
        this.vm.isShowTable = false;
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/brand/page/list', params);
        this.vm.isShowTable = true;
        return result;
    }
    async getBrand(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/brand/list', params);
        return result;
    }
    //添加品牌信息
    async addBrand(params: any) {
        await axios.post('mshop/api/gataway/item/admin/manager/brand/add', params);
    }
    //修改品牌
    async updateBrand(params: any) {
        await axios.post('mshop/api/gataway/item/admin/manager/cms/content/update', params);
    }
    //根据品牌id查询品牌对应的分类
    async getCategoryByBrandId(params: any) {
        let dataResult: any = await axios.post('mshop/api/gataway/item/admin/manager/brand/getCategoryIdByBrandId', params);
        return dataResult;
    }
    //保存数据
    async batchSaveBrandAndCategoryRls(params: any) {
        await axios.post('mshop/api/gataway/item/admin/manager/brand/batchSaveBrandAndCategoryRls', params);
    }
    //查询规格参数列表
    async getSpecByPage(params: any) {
        let dataResult: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/page/list', params);
        return dataResult;
    }
    //保存规格参数信息
    async saveParams(params: any) {
        let dataResult: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/save', params);
        return dataResult;
    }
    //批量删除规格参数
    async deleteParams(ids: Array<any>) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/delete', ids);
        return result;
    }
    //修改规格参数
    async updateParams(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/update', params);
        return result;
    }
    //根据ids修改
    async updateParamsByIds(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/updateByIds', params);
        return result;
    }
    async getTemplateByCid(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/getTemplateByCid', params);
        return result;
    }
    //分页获取商品信息
    async getSpuListPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/goods/page/list', params);
        return result;
    }
    //保存商品信息
    async saveGood(spuParam: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/goods/save', spuParam);
        return result;
    }
    //修改商品信息
    async updateGood(spuParam: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/goods/update', spuParam);
        return result;
    }
    //按条件查询Spu
    async selectProduct(spuParam: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/goods/single', spuParam);
        return result;
    }
    async deleteGoods(ids: Array<any>) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/goods/delete', ids);
        return result;
    }
    async addJdCategories() {
        let result: any = await axios.get('mshop/api/gataway/item/admin/manager/jdcategory/get');
        return result;
    }
    //分页查询首页CMS分类信息
    async getCmsCategoryPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/category/content/page/list', params);
        return result;
    }
    //不分页查询
    async getCmsCategory(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/category/content/list', params);
        return result;
    }
    //分页查询CMS内容信息
    async getCmsContentPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/content/page/list', params);
        result.data.rows.forEach(item => {
            let pics = item.pic;
            let picListArray = new Array();
            if (pics) {
                let picList = pics.split(";");
                picList.forEach(p => {
                    if (p) {
                        picListArray.push(p);
                    }
                });
            }
            item.picList = picListArray;
        });
        return result;
    }
    //不分页查询cms信息
    async getCmsContent(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/content/list', params);
        let result1: any = await axios.post('mshop/api/gataway/item/search/test');
        return result;
    }
    async asyncJdCategoryGoods(row: any, page: any) {
        let params: any = {
            categoryId: row.id,
            categoryIds: row.sort.split("-"),
            page: parseInt(page)
        };
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/async/jdcategory/goods', params);
        return result;
    }
    //添加cms分类信息
    async addCmsCategory(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/category/content/add', params);
        return result;
    }
    //批量添加cms信息
    async addCmsCategoryBatch(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/category/content/add/batch', params);
        return result;
    }
    //添加cms信息
    async addCmsContent(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/content/add', {
            categoryId: params.categoryId,
            title: params.title,
            url: params.url,
            pic: params.pic,
            status: params.status,
            sortOrder: params.sortOrder
        });
        return result;
    }
    //批量添加cms信息
    async addCmsContentBatch(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/content/add/batch', params);
        return result;
    }
    //修改cms分类信息
    async updateCmsCategoryContent(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/category/content/update', params);
        return result;
    }
    //修改cms信息
    async updateCmsContent(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/content/update', {
            id: params.id,
            categoryId: params.categoryId,
            title: params.title,
            url: params.url,
            pic: params.pic,
            status: params.status,
            sortOrder: params.sortOrder
        });
        return result;
    }
    //删除cms分类信息
    async deleteCmsCategory(ids: []) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/category/content/delete', ids);
        return result;
    }
    //删除cms信息
    async deleteCmsContent(ids: []) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/content/delete', ids);
        return result;
    }
    //同步京东cms信息
    async asyncJdCmsData() {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/async/jdcmsdata');
        return result;
    }
    async asyncTemplete(row: any) {
        let params: any = {
            categoryId: row.id,
            categoryIds: row.sort.split("-"),
            page: 1
        };
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/async/jdcategory/templete', params);
        return result;
    }
    async asyncBrand(row: any) {
        let params: any = {
            categoryId: row.id,
            categoryIds: row.sort.split("-"),
            page: 1
        };
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/cms/async/jd/brand', params);
        return result;
    }
    //删除图片信息
    async deleteImage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/file/delete', params);
        return result;
    }
    //验证是否登录
    async verify() {
        let result: any = await axios.get('mshop/api/gataway/item/admin/auth/verify');
        return result;
    }
    async login(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/auth/login', params);
        return result;
    }
    //退出登录
    async unLogin() {
        let result: any = await axios.post('mshop/api/gataway/item/admin/auth/unaccredit');
        return result;
    }
    //分页获取用户
    static async getSysUserPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/page/list', params);
        return result;
    }
    //保存用户信息
    static async saveUser(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/add', params);
        return result;
    }
    //删除用户列表
    static async deleteUser(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/delete', params);
        return result;
    }
    //修改用户信息
    static async updateUser(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/update', params);
        return result;
    }
    //分页获取角色信息
    static async getSysRolePage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/page/list', params);
        return result;
    }
    //添加角色信息
    static async addRole(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/add', params);
        return result;
    }
    //修改角色信息
    static async updateRole(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/update', params);
        return result;
    }
    //删除角色
    static async deleteRole(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/delete', params);
        return result;
    }
    //分页查询权限数据
    static async getSysPermissionPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/permission/page/list', params);
        return result;
    }
    //不分页查询
    static async getSysPermissionByCondition(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/permission/list', params);
        return result;
    }
    //添加权限
    static async addPermission(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/permission/add', params);
        return result;
    }
    //修改权限
    static async updatePermission(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/permission/update', params);
        return result;
    }
    //删除去权限
    static async deletePermission(params: Array<any>) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/permission/delete', params);
        return result;
    }
    //添加角色权限关系
    static async addRolePermission(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/permission/add', params);
        return result;
    }
    //删除角色和权限关系
    static async deleteRolePermission(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/permission/delete', params);
        return result;
    }
    //获取角色权限
    static async getRolePermission(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/role/permission/get', params);
        return result;
    }
    //添加用户角色关系
    static async addUserRoleRls(params: any) {
        axios.post('mshop/api/gataway/item/admin/sys/user/role/add', params);
    }
    //删除用户角色关系
    static async deleteUserRoleRls(params: any) {
        await axios.post('mshop/api/gataway/item/admin/sys/role/user/delete', params);
    }
    //获取用户角色关系
    static async getUserRoleRls(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/role/get', params);
        return result;
    }
    //查询用户权限
    static async getUserPermission() {
        let result: any = await axios.get('mshop/api/gataway/item/admin/sys/get/permissionByUserId');
        return result;
    }
    //修改后台用户密码
    static async updatePassword(params: any) {
        const hashPassword = md5(params.password);
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/password/update', { password: hashPassword });
        return result;
    }
    //初始化密码
    static async initPassword(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/sys/user/password/init', { username: params.username });
        return result;
    }
    static async getDefPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/workflow/list', params);
        return result;

    }
}
//定义一个拦截器，如果根据网关返回的错误码返回看看是否跳转到登录页面
axios.interceptors.response.use(res => {
    let curUrl: any = res.config.url;
    if (curUrl.indexOf("mshop/api/gataway/item/admin") > -1) {
        let whilteSite = Constants.getWhilteAdminSite();
        for (let i = 0; i < whilteSite.length; i++) {
            if ((res.data.statusCode == 403 || res.data == 403) && curUrl.indexOf(whilteSite[i]) == -1) {
                router.push({
                    path: '/login'
                });
                return;
            }
        }
        if (res.data == 100000) {
            router.push({
                path: '/login'
            });
            return;
        }
        if (res.data == 10001) {
            window.location.href = "/portal/#/admin/noPermissionPage?id=0";
            return;
        }
    }
    return res;
}, error => {

}
);


